/*
 * @Description: 
 * @Author: paddies
 * @Date: 2024-05-08 14:43:27
 * @LastEditors: paddies
 * @LastEditTime: 2025-09-18 15:55:50
 * @FilePath: /luntai-business-app/Users/paddies/Desktop/code/A-team/luntai/shared_components/lib/build-select/buildSelect.dart
 */
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared_components/build-button/buildButton.dart';
import 'package:shared_components/build-common/buildCommon.dart';
import 'package:shared_components/build-input/buildInput.dart';

getLabel({List list = const [], List value = const []}) {
  var labels = [];
  for (var i = 0; i < value.length; i++) {
    var index = list.indexWhere((element) => element['value'] == value[i]);
    if (index != -1) {
      labels.add(list[index]['label']);
    }
  }
  return labels.join(",");
}

Widget buildSelect(
    {String title = '名称',
    bool multiple = false,
    bool required = false,
    bool canInput = false,
    Color activeColor = const Color(0xff4798FB),
    Color defaultColor = const Color(0xff1d1c20),
    double labelWidth = 90,
    List list = const [],
    List value = const [],
    String bottomSheetTitle = "请选择",
    String placeholder = '请选择',
    Function? itemWidget,
    Widget topWidget = const SizedBox(),
    Function? onChanged}) {
  var showLabel = "".obs;

  showLabel.value = getLabel(list: list, value: value);

  return Container(
    width: double.infinity,
    height: 60,
    padding: EdgeInsets.only(left: 10, right: 10),
    // margin: const EdgeInsets.only(left: 15, right: 15),
    decoration: const BoxDecoration(
      border: Border(
          top: BorderSide(width: 1.0, color: Color(0xffF4F4F4)),
          bottom: BorderSide(width: 1.0, color: Color(0xffF4F4F4))),
    ),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Container(
          width: labelWidth,
          alignment: Alignment.centerLeft,
          height: double.infinity,
          // color: Colors.green,
          child: Row(
            children: [
              Visibility(
                  visible: required,
                  child: const Padding(
                    padding: EdgeInsets.only(top: 5),
                    child: Text(
                      "*",
                      style: TextStyle(fontSize: 20, color: Colors.red),
                    ),
                  )),
              Text(
                title,
                style: TextStyle(fontSize: 16),
              )
            ],
          ),
        ),
        Expanded(
            flex: 1,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Expanded(
                  child: GestureDetector(
                    onTap: () {
                      var selectListPre = List.from(value);
                      print("打开前=====");
                      print(value);
                      if (canInput) {
                        openInputPanel();
                        return;
                      }
                      openPanel(
                          list: list,
                          multiple: multiple,
                          activeColor: activeColor,
                          topWidget: topWidget,
                          itemWidget: itemWidget,
                          defaultColor: defaultColor,
                          selectListPre: selectListPre,
                          bottomSheetTitle: bottomSheetTitle,
                          submitFn: (v) {
                            //查找中文显示

                            if (onChanged != null) {
                              var n = getLabel(list: list, value: v);
                              onChanged(v, n);
                              // value = v;
                            }
                          });
                    },
                    child: Container(
                      width: double.infinity,
                      height: 60,
                      // color: Colors.red,
                      padding: EdgeInsets.only(right: 10),
                      alignment: Alignment.centerRight,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Visibility(
                              visible: value.isEmpty,
                              child: Text(
                                placeholder,
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(
                                  fontSize: 16,
                                ),
                              )),
                          Visibility(
                              visible: value.isNotEmpty,
                              child: Text(
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                showLabel.value,
                                style: TextStyle(fontSize: 16),
                              )),
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ))
      ],
    ),
  );
}

openInputPanel() {
  var screenBottomHeight = MediaQuery.of(Get.context!).padding.bottom;
  Get.bottomSheet(
      isScrollControlled: true,
      Container(
        padding: EdgeInsets.only(left: 10, right: 10, top: 20),
        decoration: new BoxDecoration(
          color: Colors.white,
          // new Radius.circular(20.0)
          borderRadius: new BorderRadius.only(
              topLeft: new Radius.circular(20.0),
              topRight: new Radius.circular(20.0)), //圆角
        ),
        height: 600,
        child: Container(
            width: double.infinity,
            height: double.infinity,
            color: Colors.red,
            child: SingleChildScrollView(
              child: Column(
                children: [
                  Container(
                    width: double.infinity,
                    alignment: Alignment.center,
                    child: Text(
                      "请输入物流公司",
                      style: TextStyle(fontSize: 18),
                    ),
                  ),
                  buildInput(
                      title: "物流公司名称",
                      onChanged: (v) {
                        print("输入的值====");
                      }),
                ],
              ),
            )),
      ));
}

openPanel(
    {List list = const [],
    bool multiple = false,
    String bottomSheetTitle = "请选择",
    Color activeColor = const Color(0xff4798FB),
    Color defaultColor = const Color(0xff1d1c20),
    Function? itemWidget,
    Widget topWidget = const SizedBox(),
    Function? submitFn,
    List selectListPre = const []}) {
  var mySelectListPre = [].obs;
  mySelectListPre.value = selectListPre;
  print("打开的值====");
  print(selectListPre);
  var screenBottomHeight = MediaQuery.of(Get.context!).padding.bottom;
  // var screenBottomHeight =
  // double.parse(GlobalPrefs.getValue("screenBottomHeight"));
  Get.bottomSheet(
      isScrollControlled: true,
      Container(
          padding: EdgeInsets.only(left: 10, right: 10, top: 20),
          decoration: new BoxDecoration(
            color: Colors.white,
            // new Radius.circular(20.0)
            borderRadius: new BorderRadius.only(
                topLeft: new Radius.circular(20.0),
                topRight: new Radius.circular(20.0)), //圆角
          ),
          height: 600,
          child: Obx(() => Container(
              width: double.infinity,
              height: double.infinity,
              // color: Colors.red,
              child: Column(
                children: [
                  Container(
                    width: double.infinity,
                    alignment: Alignment.center,
                    child: Text(
                      bottomSheetTitle,
                      style:
                          TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                    ),
                  ),
                  Expanded(
                      child: Container(
                    width: double.infinity,
                    height: double.infinity,
                    child: SingleChildScrollView(
                        child: Column(
                      children: [
                        topWidget,

                        // Text(showText(mySelectListPre.value)),
                        ...list.map((e) {
                          return Visibility(
                              visible: !(showText(e["hidden"]) == 'true'),
                              child: GestureDetector(
                                onTap: () {
                                  if (!multiple) {
                                    if (!mySelectListPre.value
                                        .contains(e["value"])) {
                                      mySelectListPre.value = [];
                                      mySelectListPre.add(e["value"]);
                                    }
                                  } else {
                                    //多选
                                    if (!mySelectListPre.value
                                        .contains(e["value"])) {
                                      mySelectListPre.add(e["value"]);
                                    } else {
                                      //取消
                                      var index = mySelectListPre.value
                                          .indexWhere((element) =>
                                              element == e["value"]);
                                      if (index != -1) {
                                        mySelectListPre.removeAt(index);
                                      }
                                    }
                                  }
                                },
                                child: Container(
                                  width: double.infinity,
                                  constraints: BoxConstraints(minHeight: 50),
                                  // height: 50,
                                  decoration: BoxDecoration(
                                    color: Colors.white,
                                    border: Border(
                                        // top: BorderSide(
                                        //     width: 1.0, color: Color(0xffF4F4F4)),
                                        bottom: BorderSide(
                                            width: 1.0,
                                            color: Color(0xffF4F4F4))),
                                  ),
                                  alignment: Alignment.centerLeft,
                                  padding: EdgeInsets.only(
                                      left: 10, right: 10, top: 5, bottom: 5),
                                  child: itemWidget != null
                                      ? itemWidget(
                                          e,
                                          mySelectListPre.value
                                              .contains(e["value"]))
                                      : Row(
                                          children: [
                                            Container(
                                              width: 40,
                                              // height: double.infinity,
                                              // color: Colors.red,
                                              child: Visibility(
                                                  visible: mySelectListPre.value
                                                      .contains(e["value"]),
                                                  child: Icon(
                                                    Icons.check,
                                                    color: activeColor,
                                                  )),
                                            ),
                                            // SizedBox(
                                            //   width: 20,
                                            // ),
                                            Expanded(
                                                child: Container(
                                                    width: double.infinity,
                                                    // height: double.infinity,
                                                    alignment:
                                                        Alignment.centerLeft,
                                                    child: Text(
                                                      showText(e["label"]),
                                                      maxLines: 4,
                                                      overflow:
                                                          TextOverflow.ellipsis,
                                                      style: TextStyle(
                                                          fontSize: 16,
                                                          color: mySelectListPre
                                                                  .value
                                                                  .contains(e[
                                                                      "value"])
                                                              ? activeColor
                                                              : defaultColor),
                                                    )))
                                          ],
                                        ),
                                ),
                              ));
                        }).toList()
                      ],
                    )),
                  )),
                  Container(
                    width: double.infinity,
                    height: 80,
                    color: Colors.white,
                    child: Row(
                      children: [
                        // Expanded(
                        //   child: Container(
                        //     width: double.infinity,
                        //     height: double.infinity,
                        //     color: Colors.white,
                        //     padding: EdgeInsets.only(left: 15),
                        //     child: Column(
                        //       mainAxisAlignment: MainAxisAlignment.center,
                        //       children: [
                        //         Row(
                        //           children: [
                        //             Text(
                        //               "合计 ",
                        //               style: TextStyle(fontSize: 18),
                        //             ),
                        //             Text(
                        //               "¥",
                        //               style: TextStyle(
                        //                   color: Colors.red,
                        //                   fontSize: 16,
                        //                   fontWeight: FontWeight.bold),
                        //             ),
                        //             Text(
                        //               showText("000"),
                        //               style: TextStyle(
                        //                   fontSize: 20,
                        //                   fontWeight: FontWeight.bold,
                        //                   color: Colors.red),
                        //             )
                        //           ],
                        //         )
                        //       ],
                        //     ),
                        //   ),
                        // ),
                        Expanded(
                            child: Container(
                          width: double.infinity,
                          height: double.infinity,
                          color: Colors.white,
                          padding: EdgeInsets.only(
                              top: 10, bottom: 10, left: 10, right: 10),
                          child: buildButton(
                              title: "确定",
                              onClick: () {
                                if (submitFn != null) {
                                  submitFn(mySelectListPre.value);
                                  Get.back();
                                }
                                // handleVisitorSeviceSubmit();
                                // Get.back();
                              },
                              colors: [Color(0xff0000f4), Color(0xff0000f4)]),
                        ))
                      ],
                    ),
                  ),
                  Container(
                    width: double.infinity,
                    height: screenBottomHeight,
                  )
                ],
              )))));
}
